const { WebSocketServer } = require("ws")

const wsSever = new WebSocketServer({ port: 3000 })

const list = new Set();

wsSever.on("connection", curWs => {
  console.log("有客户端连接我了~");

  // 真实中，不可能一直add，还需要被delete
  list.add(curWs)

  curWs.on("message", msg => {
    console.log("收到了客户端的信息：", msg.toString());

    // 服务器需要把数据广播给其它的客户端
    list.forEach(ws => {
      if (ws === curWs) return;
      ws.send(msg.toString())
    })
  })


})

